import requests
import json
import os
import keys
import CreatMemory

files = "keys.json"
APIKey = "none"
SecretKey = "none"

def get_access_token():
    """
    使用 API Key，Secret Key 获取access_token，替换下列示例中的应用API Key、应用Secret Key
    """

    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="+ APIKey +"&client_secret=" + SecretKey
    
    payload = json.dumps("")
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json().get("access_token")

def main():
    if os.path.exists(files) == False:
        Creat_data()
        Read_data()
    else:
        Read_data()
    
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + get_access_token()
    
    sentmassage = input("sent:")

    senting = {
        "messages": [
            {
                "role": "user",
                "content": sentmassage
            },
        ],
    }

    payload = json.dumps(senting)
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)

if __name__ == '__main__':
    main()
